package com.ias.agdis_r.fragment.task;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
//import android.support.v4.app.Fragment;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.action.task.TaskOprateAction;
import com.ias.agdis_r.activity.flight.SearchInfoActivity;
import com.ias.agdis_r.activity.message.MessageSendActivity;
import com.ias.agdis_r.activity.message.MessageTaskReceived;
import com.ias.agdis_r.activity.task.FlightSummaryActivity;
import com.ias.agdis_r.activity.task.TaskDocListActivity;
import com.ias.agdis_r.activity.task.TaskMonitorActivity;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.bean.other.TempLabelBean;
import com.ias.agdis_r.bean.task.Task;
import com.ias.agdis_r.bean.task.TaskProcess;
import com.ias.agdis_r.bean.task.TaskServiceLabel;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.temple.ButtonTempBean;
import com.ias.agdis_r.temple.TaskDataTemp;
import com.ias.agdis_r.temple.TaskTempBean;
import com.ias.agdis_r.temple.XmlHelper;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.request.SubmittaskvnbRequest;
import com.ias.agdis_r.terminalmanager.restful.task.vo.SubmitTaskVnbRequestVO;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.TaskUtil;
import com.ias.agdis_r.util.TaskUtil.MonitorButtonStatus;
import com.ias.agdis_r.util.network.HttpUtil;
import com.ias.agdis_r.util.sdcard.SDcard;
import com.ias.agdis_r.view.control.Group1TypeView;
import com.ias.agdis_r.view.control.Group2TypeView;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * 任务详情_任务执行页面
 *
 * @author guoxr
 */
public class NFTaskInfoTab01 extends Fragment {
    public static final int REQUESTCODE_CAMERA = 1;
    private Task task = null;
    private boolean ishis = false;// 是否为历史任务
    private String tnb = "";
    private LinearLayout layout_taskinfo_temple = null;
    private Group1TypeView group1View = null;
    private Group2TypeView group2View = null;
    private LinearLayout button_function_temple, ll_taskmenu;
    private TaskTempBean taskViewTemp;
    private View view;
    private HashMap<String, Object> taskdata;
    // 需要确认的动作
    private String confirm_action = "";
    private List<ButtonTempBean> buttonTemps;
    private int flightType = ParameterModel.FLIGHT_ARRIVAL;
    private Button btn1 = null, btn2 = null, btn3 = null, btn4 = null;
    private AlertDialog dialog;
    private ArrayList<String> addStrings = new ArrayList<String>();
    private ArrayList<ButtonTempBean> bTemp = new ArrayList<ButtonTempBean>();
    private static Context context = DataBaseManager.getInstance().getContext();
    String photo_name = "";
    private boolean hasTags = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.nftaskinfo_tab01, container, false);
        tnb = getArguments().getString(MessageModel.TNB);
        XmlHelper.getInstance().loadNFTaskXML();
        taskViewTemp = XmlHelper.getInstance().getNFTaskViewTemp();
        updateUI();
        return view;
    }

    protected void loadData() {
        Task temp = DataManager.getInstance().getWorkNFTask(tnb);
        if (null == temp) {
            temp = DataManager.getInstance().getNFHistoryWorkTask(tnb);
            if (temp != null) {
                ishis = true;
            }
        }
        if (null == temp) {
            temp = DataManager.getInstance().getReceiveNFTask(tnb);
        }
        task = temp;
    }

    /**
     * 载入模板、更新UI
     */
    public void updateUI() {
        ll_taskmenu = (LinearLayout) view.findViewById(R.id.ll_taskmenu);
        // 加载数据
        loadData();
        if (task == null) {
            EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.noTask), false);
            // back();
            return;
        }
        TextView tnm = (TextView) view.findViewById(R.id.txtTNM);
        tnm.setText(task.getTaskInfo().getTNM());
        TextView ttp = (TextView) view.findViewById(R.id.txtTTP);
        ttp.setText(task.getTaskInfo().getTTP());
        // 下一任务节点
        TaskProcess nextProcess = TaskUtil.getNextTaskNode(task);
        if (ishis) {
            ll_taskmenu.setVisibility(View.INVISIBLE);
            // 加载模板
            loadUITemp("T_HIST", null);
            // 显示数据
            bindData();
        } else {
            // 如果没有下一任务节点
            if (nextProcess == null) {
                // back();
                EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.taskDone), false);
            } else {
                // 加载模板
                loadUITemp(task.getPID(), null);
                // 显示数据
                bindData();
            }
        }
    }

    /**
     * 加载UI模板
     */
    public void loadUITemp(String pid, String aod) {
        Log.i("TaskInfoTab01", "加载任务模板PID:" + pid + "|AOD:" + aod);
        try {
            if (task != null && taskViewTemp != null) {
                // 加载布局文件中taskinfo的layout
                layout_taskinfo_temple = (LinearLayout) view
                        .findViewById(R.id.taskinfo_temple);
                // 加载布局文件中button的layout
                // layout_button_temple = (LinearLayout) view
                // .findViewById(R.id.button_temple);
                layout_taskinfo_temple.removeAllViews();
                // layout_button_temple.removeAllViews();

                TaskDataTemp taskDataTemp = null;
                // 遍历模板节点
                Iterator<String> iterator = taskViewTemp.taskDataList.keySet()
                        .iterator();
                while (iterator.hasNext()) {
                    String key = (String) iterator.next();
                    // 如果当前节点不为空，且内容中包含此节点
                    if (S.isNotEmpty(pid) && key.indexOf(pid) > -1) {
                        taskDataTemp = taskViewTemp.taskDataList.get(key);
                        break;
                    }
                }
                if (null==taskDataTemp) {
                    taskDataTemp = taskViewTemp.taskDataList.get("T_DEFAULT");
                }
                if (taskDataTemp != null) {
                    buttonTemps = taskDataTemp.buttonTemps;
                   // AodTypeItem aodItem = taskDataTemp.aodTypeList.get(aod);
                    if (buttonTemps.size() > 0) {
                        addButton(buttonTemps);
                    }
//                    if (aodItem != null) {
//                        addUIItemGroup1(aodItem.group1);
//                        ImageView line = new ImageView(getActivity()
//                                .getApplicationContext());
//                        line.setBackgroundResource(R.drawable.line2);
//                        layout_taskinfo_temple.addView(line);
                    hasTags = false;
                        HashMap<String, TaskServiceLabel> serviceLabelMap = task.getServiceLabelMap();
                        Set<String> keySet = serviceLabelMap.keySet();
                        LinkedHashMap<String, TempLabelBean> itemList = new LinkedHashMap<String, TempLabelBean>();
                        for (Object key : keySet.toArray()) {
                            TaskServiceLabel serviceLabel = serviceLabelMap.get(key);
                            // 如果是通用任务标注项，则不显示到模板，显示到标注列表中。
                            if (serviceLabel.getIsMark()) {
                                hasTags = true;
                                continue;
                            }
                            TempLabelBean tempLabelBean = new TempLabelBean();
                            tempLabelBean.setContent(serviceLabel.getMKNM());
                            itemList.put(serviceLabel.getMKID(), tempLabelBean);
                        }
                        addUIItemGroup2(itemList);
//                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("TaskInfoTab01", "加载模板错误");
            // EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO,
            // "加载模板错误", false);
        }

    }

    ;

    /**
     * 向UI中增加Button
     */
    public void addButton(List<ButtonTempBean> buttonTemp) {
        // 如果存在按钮
        if (buttonTemp != null && buttonTemp.size() > 0) {
            button_function_temple = (LinearLayout) view
                    .findViewById(R.id.button_function_temple);
            for (ButtonTempBean buttonTemp1 : buttonTemp) {
                // 根据模板生成按钮
                if (buttonTemp1.type.equals("left")
                        || buttonTemp1.type.equals("star")
                        || buttonTemp1.type.equals("well")
                        || buttonTemp1.type.equals("right")) {
                    // 如果按钮名称为空的,显示任务下一节点名称，针对于工作列表中的任务
                    if (S.valueOf(buttonTemp1.mapping).equals("onExcuteTask")) {
                        Task task = DataManager.getInstance().getWorkTask(tnb);
                        if (null == task){
                            task = DataManager.getInstance().getWorkNFTask(tnb);
                        }
                        // 如果任务不为空
                        if (task != null) {
                            // 获取任务下一节点
                            TaskProcess nextProcess = TaskUtil
                                    .getNextTaskNode(task);
                            // 如果任务下一节点不为空
                            if (nextProcess != null) {
                                // 加载下一节点名称
                                buttonTemp1.value = S.valueOf(nextProcess
                                        .getPCNM());
                            }
                        }
                    } else if (S.valueOf(buttonTemp1.mapping).equals(
                            "gotoTaskMonitorView")) {
                        MonitorButtonStatus buttonStatus = TaskUtil
                                .checkNFTaskMonitor(tnb);
                        // 如果有监控项内容
                        if (MonitorButtonStatus.MONITOR == buttonStatus) {
                            buttonTemp1.value = DataBaseManager.getInstance()
                                    .getContext().getString(R.string.monitor);
                        }
                        // 如果有服务标注项内容
                        else if (MonitorButtonStatus.SERVICE_LABEL == buttonStatus) {
                            buttonTemp1.value = DataBaseManager.getInstance()
                                    .getContext()
                                    .getString(R.string.service_label);
                        } else {
                            continue;
                        }
                    }
                    initButtonView(buttonTemp1, button_function_temple);
                }
            }
        }
    }

    ;

    /**
     * 初始化按钮
     */
    protected void initButtonView(ButtonTempBean buttonTemp1, LinearLayout parentLayout) {
        String textValue = "";
        if (buttonTemp1.type.equals("left")) {
            btn1 = (Button) parentLayout.findViewById(R.id.button_left);
            textValue = buttonTemp1.value;
            btn1.setVisibility(View.VISIBLE);
            btn1.setText(textValue);
            addStrings.add(textValue);
            bTemp.add(buttonTemp1);
            initButtonClickListener(btn1, buttonTemp1);
        } else if (buttonTemp1.type.equals("star")) {
            btn2 = (Button) parentLayout.findViewById(R.id.button_star);
            textValue = buttonTemp1.value;
            btn2.setVisibility(View.VISIBLE);
            btn2.setText(textValue);
            addStrings.add(textValue);
            bTemp.add(buttonTemp1);
            initButtonClickListener(btn2, buttonTemp1);
        } else if (buttonTemp1.type.equals("well")) {
            btn3 = (Button) parentLayout.findViewById(R.id.button_well);
            textValue = buttonTemp1.value;
            btn3.setVisibility(View.VISIBLE);
            btn3.setText(textValue);
            addStrings.add(textValue);
            bTemp.add(buttonTemp1);
            initButtonClickListener(btn3, buttonTemp1);
        } else if (buttonTemp1.type.equals("right")) {
            btn4 = (Button) parentLayout.findViewById(R.id.button_right);
            textValue = buttonTemp1.value;
            btn4.setVisibility(View.VISIBLE);
            btn4.setText(textValue);
            addStrings.add(textValue);
            bTemp.add(buttonTemp1);
            initButtonClickListener(btn4, buttonTemp1);
        }
        if (0 == addStrings.size()) {
            ll_taskmenu.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 初始化按钮监控事件
     */
    protected void initButtonClickListener(final Button button,
                                           final ButtonTempBean buttonTemp1) {
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clickButton(buttonTemp1);
            }
        });

        ll_taskmenu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                showDialog();
            }
        });
    }

    // 跳转到航班报告总结页面
    private void gotoFlightSummaryView(String tnb, String excutePid) {
        Task task = DataManager.getInstance().getReceiveTask(tnb);
        if (task == null) {
            task = DataManager.getInstance().getWorkTask(tnb);
        }
        if (task == null) {
            return;
        }
        Intent intent = new Intent(getActivity()
                .getApplicationContext(), FlightSummaryActivity.class);
        intent.putExtra(MessageModel.TNB, tnb);
        intent.putExtra(MessageModel.PID, task.getPID());
        intent.putExtra("EXCUTE_PID", excutePid);
        getActivity().startActivity(intent);
    }

    private void clickButton(ButtonTempBean buttonTemp1) {
        // 如果该按钮映射是执行任务,且配置了航班报告总结
//        if (buttonTemp1.mapping.equals("onExcuteTask")
//                && TaskUtil.checkSummary(tnb, null)) {
//            Log.i("TaskInfoTab01", "onExcuteTask,该按钮映射是执行任务,且配置了航班报告总结");
//            gotoFlightSummaryView(tnb, null);
//        }
//        // 如果该按钮映射是拒绝任务,且配置了航班报告总结
//        else if (buttonTemp1.mapping.equals("onRefuseTask")
//                && TaskUtil.checkSummary(tnb, MessageModel.TRJT)) {
//            Log.i("TaskInfoTab01", "onRefuseTask,该按钮映射是拒绝任务,且配置了航班报告总结");
//            gotoFlightSummaryView(tnb, MessageModel.TRJT);
//        }
//        // 如果该按钮映射是接受任务,且配置了航班报告总结
//        else if (buttonTemp1.mapping.equals("onAcceptTask")
//                && TaskUtil.checkSummary(tnb, MessageModel.TACP)) {
//            Log.i("TaskInfoTab01", "onAcceptTask,该按钮映射是接受任务,且配置了航班报告总结");
//            gotoFlightSummaryView(tnb, MessageModel.TACP);
//        }
        // 是否需要确认
        //else
        if (buttonTemp1.confirm) {
            // 保存确认的动作
            confirm_action = buttonTemp1.mapping;
            Log.i("TaskInfoTab01", "确认框动作confirm_action:"
                    + confirm_action);
            // 判断无提示框显示后，打开提示框
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
            openPropmt(buttonTemp1.value);
        } else {
            // 保存确认的动作
            confirm_action = buttonTemp1.mapping;
            // 执行动作
            Log.i("TaskInfoTab01", "无确认框，执行动作:" + buttonTemp1.mapping);
            if (confirm_action.equals("gotoDetailInfoView")) {
                // 判断无提示框显示后，打开提示框
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                DetailInfoDialog();
            } else {
                if (confirm_action.equals("bindCarNum")) {
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    bindCarNum(tnb);
                } else if (confirm_action.equals("gotoTaskMonitorView")) {
                    Intent intent = new Intent(getActivity(), TaskMonitorActivity.class);
                    intent.putExtra(MessageModel.TNB, tnb);
                    getActivity().startActivity(intent);
                } else if (confirm_action.equals("back")) {
                    getActivity().finish();
                } else {
                    TaskOprateAction.getInstance().excuteAction(
                            confirm_action, tnb, null);
                }
            }
        }
    }

    private void showDialog() {
        if (addStrings != null && bTemp != null) {
            final String[] strings = addStrings.toArray(new String[addStrings.size()]);
            final ButtonTempBean[] bT = bTemp.toArray(new ButtonTempBean[bTemp.size()]);
            AlertDialog.Builder bulider = new AlertDialog.Builder(getActivity());
            bulider.setTitle(context.getString(R.string.chooseOperat));
            bulider.setIcon(android.R.drawable.ic_dialog_info);
            bulider.setSingleChoiceItems(strings, 0,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            clickButton(bT[which]);
                            dialog.dismiss();
                        }
                    });
            bulider.setNegativeButton(context.getString(R.string.cancel), null);
            dialog = bulider.create();
            dialog.show();
        }
    }

    public void removeButton() {
        if (btn1 != null) {
            btn1.setVisibility(View.GONE);
        }
        if (btn2 != null) {
            btn2.setVisibility(View.GONE);
        }
        if (btn3 != null) {
            btn3.setVisibility(View.GONE);
        }
        if (btn4 != null) {
            btn4.setVisibility(View.GONE);
        }
        addStrings.clear();
        bTemp.clear();
    }

    /**
     * 判断是否有绑定车号功能&&是否是结束节点，是否绑定了车号
     */
    private void openPropmt(String value) {

        String alertMsg = "";
        String pid = "";
        Task task = DataManager.getInstance().getWorkTask(tnb);
        if (task != null) {
            TaskProcess taskProcess = TaskUtil.getNextTaskNode(task);
            if (taskProcess != null) {
                pid = taskProcess.getPCID();
            }
        }
        for (ButtonTempBean buttonTempBean : buttonTemps) {
            // 如果按钮有绑定车号
            if ((S.valueOf(buttonTempBean.mapping).equals("bindCarNum")
                    ||JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_USERCARBIND.toString())) && MessageModel.TEND.equals(pid)) {
                String vnb = TaskUtil.getVNBbyTNB(tnb);
                if (S.isEmpty(vnb)) {
                    alertMsg = context.getString(R.string.bindingCar);
                } else {
                    alertMsg = context.getString(R.string.TIT_1) + vnb
                            + context.getString(R.string.TIT_2);
                }
                dialog(alertMsg);
                break;
            }
        }
        if (S.isEmpty(alertMsg)) {
            dialog(context.getString(R.string.FSA_1) + value + context.getString(R.string.FSA_2));
        }
    }

    /**
     * 提示dialog
     */
    private void dialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(context.getString(R.string.prompt));
        builder.setMessage(message);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setPositiveButton(context.getString(R.string.confirm), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (S.isNotEmpty(confirm_action)) {
                    // 执行弹出确认按钮前的动作
                    TaskOprateAction.getInstance().excuteAction(confirm_action,
                            tnb, null);
                    confirm_action = "";
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                confirm_action = "";
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    /**
     * 生成模板中Group1的UI
     */
    public void addUIItemGroup1(LinkedHashMap<String, TempLabelBean> itemList) {
        Set<String> keySet = itemList.keySet();
        ProgressBar progress = null;
        group1View = new Group1TypeView(getActivity().getApplicationContext(),
                progress);
        for (Object key : keySet.toArray()) {
            TempLabelBean group1TaskLabel = (TempLabelBean) itemList.get(key);
            group1View.addViewByTag(key.toString(), group1TaskLabel);
        }
        layout_taskinfo_temple.addView(group1View);
    }

    ;

    /**
     * 生成模板中Group2的UI
     */
    public void addUIItemGroup2(LinkedHashMap<String, TempLabelBean> itemList) {
        Set<String> keySet = itemList.keySet();
        ProgressBar progress = null;
        group2View = new Group2TypeView(getActivity().getApplicationContext(),
                progress);
        for (Object key : keySet.toArray()) {
            TempLabelBean group2TaskLabel = (TempLabelBean) itemList.get(key);
            group2View.addViewByTag(key.toString(), group2TaskLabel);
        }
        // 将内容装到布局文件的layout中
        layout_taskinfo_temple.addView(group2View);
    }

    ;

    /**
     * 显示数据
     */
    protected void bindData() {
        if (task != null) {
            HashMap<String, TaskServiceLabel> serviceLabelMap = task.getServiceLabelMap();
            Set<String> keySet = serviceLabelMap.keySet();
            for (String key : keySet){
                setInfoByTag(serviceLabelMap.get(key).getMKID(), serviceLabelMap.get(key).getCTNT());
            }
//            TaskInfo taskInfo = task.getTaskInfo();
//            if (taskInfo.getAOD().equals("D")) {
//                icon_AOD.setBackgroundResource(R.drawable.adidd);
//                txt_AOD = context.getString(R.string.adid_dm);
//                flightType = ParameterModel.FLIGHT_DEPARTURE;
//            } else {
//                icon_AOD.setBackgroundResource(R.drawable.adida_red);
//                txt_AOD = context.getString(R.string.adid_am);
//                flightType = ParameterModel.FLIGHT_ARRIVAL;
//            }
//            for (TaskInfoEnum tag : TaskInfoEnum.values()) {
//                String value = taskInfo.getString(tag);
//                if (TaskInfoEnum.FLN.equals(tag)) {
//                    icon_company.setBackgroundResource(AirlineCompanyUtil
//                            .getArilineCompanyIconResourceId(value));
//                } else if (TaskInfoEnum.STN.equals(tag)) {
//                    TaskProcess pN = task.getProcess(task.getPID());
//                    if (pN != null) {
//                        value = S.valueOf(pN.getPCNM());
//                    } else {
//                        EffectManager.getInstance().ttsPlay(
//                                EffectManager.TTS_TYPE_TASK,
//                                context.getString(R.string.TIT_3) + task.getTaskInfo().getFLN() + context.getString(R.string.task)
//                                        + task.getTNB() + context.getString(R.string.TIT_4), true);
//                        return;
//                    }
//                } else if (isTimeField(tag.toString())) {
//                    value = S.FormatTime2(value);
//                } else if (TaskInfoEnum.FTS.equals(tag)) {
//                    value = getLabelContent(value);
//                } else if(TaskInfoEnum.JTT.equals(tag)) {
//                    String jao = S.valueOf(taskInfo.getString(TaskInfoEnum.JAT));
//                    String jeo = S.valueOf(taskInfo.getString(TaskInfoEnum.JET));
//                    String jso = S.valueOf(taskInfo.getString(TaskInfoEnum.JST));
//                    value = S.getFlightLatestTime(jao, jeo, jso);
//                    value = S.FormatTime2(value);
//                } else if (TaskInfoEnum.JFK.equals(tag)) {
//                    if (group1View != null) {
//                        group1View.setJFK(value);
//                    }
//                    if (group2View != null) {
//                        group2View.setJFK(value);
//                    }
//                }
//                setInfoByTag(tag.toString(), value);
//            }
        }
    }

    /**
     * 根据TAG，为VIEW赋值
     */
    protected boolean setInfoByTag(String infoTag, String infoValue) {
        if (S.isEmpty(infoTag)) {
            return false;
        }
        TextView mview = (TextView) view.findViewWithTag(infoTag);
        if (mview != null && infoValue != null) {
            mview.setTextColor(Color.BLACK);
            mview.setText(infoValue);
            if (infoTag.equals("JFN") && S.isNotEmpty(infoValue) && S.isNotEmpty(task.getTaskInfo().getJFK())){
                mview.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                mview.setTextColor(getResources().getColor(R.color.p_bule));
                mview.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), SearchInfoActivity.class);
                        intent.putExtra(SearchInfoActivity.EXTRA_FLIGHT_JFK, task.getTaskInfo().getJFK());
                        intent.putExtra(SearchInfoActivity.EXTRA_TITLE, getString(R.string.jfFlight));
                        getActivity().startActivity(intent);
                    }
                });
            }
            return true;
        } else {
            return false;
        }
    }

    private boolean isTimeField(String type) {
        if (type.equals("ATO") || type.equals("STO") || type.equals("ETO")
                || type.equals("APB") || type.equals("APO")
                || type.equals("FDT") || type.equals("PUI")
                || type.equals("PUO") || type.equals("JAT") || type.equals("JST")||
                type.equals("JET")||type.equals("OPT")) {
            return true;
        }
        return false;
    }

    /**
     * 显示航班标注的内容
     */
    private String getLabelContent(String content) {
        if ("X".equals(content)) {
            content = context.getString(R.string.cancel);
        } else if ("Y".equals(content)) {
            content = context.getString(R.string.late);
        } else {
            content = context.getString(R.string.normal);
        }
        return content;
    }

    // 信息Dialog
    private void DetailInfoDialog() {
        final String[] strings = initstrings();
        taskdata();
        AlertDialog.Builder bulider = new AlertDialog.Builder(getActivity());
        bulider.setTitle(task.getTaskInfo().getTNM());
        bulider.setIcon(android.R.drawable.ic_dialog_info);
        bulider.setSingleChoiceItems(strings, 0,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
//                        switch (strings[which]) {
//                            case "发送信息":
//                                Intent intent = new Intent(getActivity(),
//                                        MessageSendActivity.class);
//                                intent.putExtra(MessageSendActivity.EXTRA_TYPE,
//                                        MessageSendActivity.TYPE_TASK);
//                                intent.putExtra(MessageSendActivity.EXTRA_TNB, tnb);
//                                intent.putExtra(MessageSendActivity.EXTRA_FKE, task
//                                        .getTaskInfo().getFKE());
//                                intent.putExtra(MessageSendActivity.EXTRA_FLN, task
//                                        .getTaskInfo().getFLN());
//                                intent.putExtra(MessageSendActivity.EXTRA_AOD, task
//                                        .getTaskInfo().getAOD());
//                                getActivity().startActivity(intent);
//                                break;
//                            case "已收信息":
//                                Intent intent1 = new Intent(getActivity(), MessageTaskReceived.class);
//                                intent1.putExtra(MessageTaskReceived.EXTRA_FKE,task.getTaskInfo().getFKE());
//                                getActivity().startActivity(intent1);
//                                break;
//                            case "打印单据":
//                                Intent billintent = new Intent(getActivity()
//                                        .getApplicationContext(),
//                                        TaskDocListActivity.class);
//                                billintent.putExtra("taskdata", taskdata);
//                                startActivity(billintent);
//                                break;
//                            case "手机拍照":
//                                startCamera();
//                                break;
//                            default:
//                                break;
//                        }
                        if (strings[which].equals(context.getString(R.string.sendMessage))) {
                            Intent intent = new Intent(getActivity(), MessageSendActivity.class);
                            intent.putExtra(MessageSendActivity.EXTRA_TYPE, MessageSendActivity.TYPE_TASK);
                            intent.putExtra(MessageSendActivity.EXTRA_TNB, tnb);
                            intent.putExtra(MessageSendActivity.EXTRA_FKE, task.getTaskInfo().getFKE());
                            intent.putExtra(MessageSendActivity.EXTRA_FLN, task.getTaskInfo().getFLN());
                            intent.putExtra(MessageSendActivity.EXTRA_AOD, task.getTaskInfo().getAOD());
                            getActivity().startActivity(intent);
                        } else if (strings[which].equals(context.getString(R.string.receiveMessage))) {
                            Intent intent1 = new Intent(getActivity(), MessageTaskReceived.class);
                            intent1.putExtra(MessageTaskReceived.EXTRA_FKE, task.getTaskInfo().getFKE());
                            getActivity().startActivity(intent1);
                        } else if (strings[which].equals(context.getString(R.string.docPrint))) {
                            Intent billintent = new Intent(getActivity().getApplicationContext(), TaskDocListActivity.class);
                            billintent.putExtra("taskdata", taskdata);
                            getActivity().startActivity(billintent);
                        } else if (strings[which].equals(context.getString(R.string.takePicture))) {
                            startCamera();
                        }
                        dialog.dismiss();
                    }
                });
        bulider.setNegativeButton(context.getString(R.string.cancel), null);
        dialog = bulider.create();
        dialog.show();
    }

    /**
     * 启动手机拍照界面
     */
    public void startCamera() {

        if (!SDcard.isFileExist(DataBaseManager.PHOTO_FORDER_PATH)) {// 如果目录不存在，则创建目录
            SDcard.creatSDDir(DataBaseManager.PHOTO_FORDER_PATH);
        }

        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addCategory("android.intent.category.DEFAULT");
        String subname = "";

        if (S.isNotEmpty(tnb)) {
            subname = tnb;
        }
        if (S.isNotEmpty(task.getTaskInfo().getFLN())) {
            subname = task.getTaskInfo().getFLN() + "_" + subname;
        }
		String photoPath = DataBaseManager.PHOTO_FORDER_PATH + S.getPhotoName(subname);
        File file = new File(photoPath);
        Uri uri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        getActivity().startActivityForResult(intent, REQUESTCODE_CAMERA);
    }

    private String[] initstrings() {
        List<String> list = new ArrayList<String>();
        list.add(context.getString(R.string.sendMessage));
        list.add(context.getString(R.string.receiveMessage));
        if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_DOCPRINT.toString())) {
            list.add(context.getString(R.string.docPrint));
        }
        if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_CAMERA.toString())) {
            list.add(context.getString(R.string.takePicture));
        }
        String[] strings = (String[]) list.toArray(new String[list.size()]);
        return strings;
    }

    private void taskdata() {
        // TODO Auto-generated method stub
        taskdata = new HashMap<String, Object>();
        taskdata.put(MessageModel.TNB, task.getTNB());
        taskdata.put(ParameterModel.QINGKUANGFANYING_TYPE,
                ParameterModel.QINGKUANGFANYING_TASK);
        taskdata.put(ParameterModel.TYPE, ParameterModel.TASK_SHENGCHANTONGGAO);
        taskdata.put(ParameterModel.FLIGHT_TYPE, flightType);
        taskdata.put(MessageModel.strTxtValue[MessageModel.nFKE], task
                .getTaskInfo().getFKE());
        taskdata.put(MessageModel.strTxtValue[MessageModel.nFLN], task
                .getTaskInfo().getFLN());
        taskdata.put(MessageModel.strTxtValue[MessageModel.nAOD], task
                .getTaskInfo().getAOD());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUESTCODE_CAMERA && resultCode != Activity.RESULT_CANCELED) {
            //保存缩略图
            makeImageThumbnails();
            //重新启动相机
            startCamera();
        }
    }

    /**
     * 保存照片缩略图
     */
    public void makeImageThumbnails() {
        BufferedOutputStream bos = null;
        Bitmap bitmap = null;
        try {
            if (S.isEmpty(photo_name)) {
                return;
            }
            String photoPath = DataBaseManager.PHOTO_FORDER_PATH + photo_name;
            File file = new File(photoPath);
            //如果没有图片文件，返回
            if (!file.exists()) {
                return;
            }
            String thumbnails_path = DataBaseManager.DCIM_FORDER_PATH + photo_name;
            //如果目录不存在创建目录
            if (!SDcard.isFileExist(DataBaseManager.DCIM_FORDER_PATH)) {
                SDcard.creatSDDir(DataBaseManager.DCIM_FORDER_PATH);
            }
            //生成缩略图
            if (!SDcard.isFileExist(thumbnails_path)) {

                SDcard.creatSDFile(thumbnails_path);
            }
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true; // 设为 false
            bitmap = BitmapFactory.decodeFile(photoPath, options);
            options.inJustDecodeBounds = false; // 设为 false
            // 计算缩放比
            int h = options.outHeight;
            int w = options.outWidth;
            int beWidth = w / 90;
            int beHeight = h / 90;
            int be = 1;
            if (beWidth < beHeight) {
                be = beWidth;
            } else {
                be = beHeight;
            }
            if (be <= 0) {
                be = 1;
            }
            options.inSampleSize = be;
            // 重新读入图片，读取缩放后的bitmap，注意这次要把options.inJustDecodeBounds 设为 false
            bitmap = BitmapFactory.decodeFile(photoPath, options);
            bos = new BufferedOutputStream(new FileOutputStream(thumbnails_path));
            //OutputStream os = new FileOutputStream(myCaptureFile);
            /* 采用压缩转档方法 */
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            /* 调用flush()方法，更新BufferStream */
            bos.flush();

			/* 结束OutputStream */
            bos.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
        }

    }

    /**
     * 绑定车号
     *
     * @param
     */
    public void bindCarNum(final String tnb) {
        AlertDialog.Builder dialogb = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog, null);
        dialogb.setTitle(context.getString(R.string.inputCarnum));
        EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.inputCarnum), false);
        dialogb.setView(layout);
        dialogb.setIcon(android.R.drawable.ic_dialog_info);
        final EditText txt_monitor_content = (EditText) layout.findViewById(R.id.edit_content);
        dialogb.setPositiveButton(context.getString(R.string.confirm), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String carNum = txt_monitor_content.getText().toString();
                if (carNum.contains("@") || carNum.contains("#") || carNum.contains("&") || carNum.contains(" ")) {
                    EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO, context.getString(R.string.inputError2), false);
                    return;
                }
                if (carNum.length() > 20) {
                    EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO, context.getString(R.string.lessThan20), false);
                    return;
                }
                // 上报绑定的车牌号
                SubmittaskvnbRequest vnbrRequest = new SubmittaskvnbRequest();
                initTRBD(vnbrRequest, tnb, carNum);
                HttpUtil.getInstance().sendRequest(vnbrRequest, MessageModel.TRBD, true);
            }
        });
        dialogb.setNegativeButton(context.getString(R.string.cancel), null);
        dialog = dialogb.create();
        dialog.show();
    }

    private void initTRBD(SubmittaskvnbRequest vnbrRequest, String tnb,
                          String vnb) {
        // TODO Auto-generated method stub
        SubmitTaskVnbRequestVO vnbRequestVO = new SubmitTaskVnbRequestVO();
        String tst;
        vnbRequestVO.setTNB(tnb);
        vnbRequestVO.setVNB(vnb);
        if (S.isNotEmpty(vnb)) {
            tst = "Y";
        } else {
            tst = "N";
        }
        vnbRequestVO.setTST(tst);
        vnbrRequest.setSubmittaskVnbRequestVO(vnbRequestVO);
    }
}
